using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Data;
using System.Windows.Forms;

using System.Net;


using Hydrobase;
namespace RSSApplicationUpdater
{
    public class Core
    {
        /// <summary>
        /// Base class that handles exceptions , returns the version and reads the config
        /// </summary>
        static TheDarkOwlLogger.TheDarkOwlLogger Bugger = new TheDarkOwlLogger.TheDarkOwlLogger(true,true,true,"RSSApplicationUpdater");
        public const string configfile = "updater.xml";
        public const string updatesfolder = "Updates";
        public static string[] cells = {"application_title","upadefeed","Release_State","CMS","Auto_Check","Application_WebSite" 
                                           ,"alpha_feed","beta_feed","rc_feed","stable_feed"};
        public static hydrobaseADO ado = new hydrobaseADO();
        public static DataSet set = new DataSet();
        /// <summary>
        /// This method handles the given exeption.
        /// It send to the programmer
        /// </summary>
        /// <param name="ex">The exception that is to be handled</param>
        public static void errorreport(Exception ex)
        {

            WebException webext = new WebException("The remote name could not be resolved");
            
           if((ex.Message.Contains( webext.Message)==true)||(ex.GetType()== webext.GetType())  )
           {
               MessageBox.Show(ex.Message );

           }
           else
           {

              
            Bugger.TakeTheException2XmlLog(ex,"RSSApplicationUpdater",GetVersion());
           }

        }
        /// <summary>
        /// Returns the version of this library
        /// </summary>
        /// <returns>Returns the version of this library</returns>
        public static string GetVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version.ToString();

        }
        /// <summary>
        /// Reads te information saved at updater.xml file.
        /// </summary>
        public void GetConfig()
        {
            try
            {
                string appfolder = Application.StartupPath;
                if (File.Exists(Application.StartupPath + "\\" + configfile)== true)
                {

                    ado.AttachDataBaseinDataSet(set, appfolder + "\\" + configfile);


                }
                else
                {


                    ado.TableCreation(appfolder + "\\" + configfile, cells, Application.ProductName + " - " + Application.ProductVersion);

                }


            }
            catch (Exception e)
            {

                errorreport(e);
            }


        }
        public void ChangeConfigFeed()
        {
            try
            {
                WebBrowser bro = new WebBrowser(true);
                bro.Text= "Change Update Feed of THe Application";
               
                bro.Show();


            }
            catch(Exception ex)
            {

                errorreport(ex);
            }


        }

    }
}
